package order

import (
	"goods-rpc/models"
	"goods-rpc/proto/order"
)

func orderModelInfo(orders *models.Order) (info *order.OrderInfo, err error) {
	return &order.OrderInfo{
		OrderNo:     orders.OrderNo,
		OrderCount:  float32(orders.OrderCount),
		OrderNum:    int64(orders.OrderNum),
		GoodsId:     int64(orders.GoodsId),
		UserId:      int64(orders.UserId),
		CreatedAt:   orders.CreatedAt,
		OrderStatus: int64(orders.OrderStatus),
	}, nil
}

func orderModelList(orders []models.Order) (info []*order.OrderInfo, err error) {
	for _, v := range orders {
		info = append(info, &order.OrderInfo{
			Id:          int64(v.ID),
			OrderNo:     v.OrderNo,
			OrderCount:  float32(v.OrderCount),
			OrderNum:    int64(v.OrderNum),
			GoodsId:     int64(v.GoodsId),
			UserId:      int64(v.UserId),
			CreatedAt:   v.CreatedAt,
			OrderStatus: int64(v.OrderStatus),
		})
	}

	return info, nil
}

func TakeAdd() (*order.OrderInfo, error) {
	m := new(models.Order)
	add, err := m.Add()
	if err != nil {
		return nil, nil
	}
	return orderModelInfo(add)
}

func TakeList() ([]*order.OrderInfo, error) {
	m := new(models.Order)
	list, err := m.List()
	if err != nil {
		return nil, nil
	}

	return orderModelList(list)
}
